#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Definition des fonctions                                     ##
#'#                                                                                              ##
#'#################################################################################################


StaticData <- setClass("StaticData", representation(visibility="character", 
        gps="data.frame", argos="data.frame"))

getError <- function (x, ...) {
    UseMethod("getError")
}

extractStaticData <- function(visibilities, write=FALSE, gpscolumns=c("Time", "Position"),
                      argoscolumns=c("Platform.ID.", "Latitude", "Longitude", "Loc..quality", "Loc..date", 
                          "Error.radius", "Semi.major.axis", "Semi.minor.axis", "Ellipse.orientation"),
											destdir=".") {
  result <- list()
	deployment <- 1
  for (visibility in visibilities) {
    data <- new("StaticData")
    vis <- substr(visibility, nchar(visibility), nchar(visibility))
    data@visibility <- vis
    sessions <- getStaticSessions(visibility)
    tmpgps <- NULL
    tmpargos <- NULL
    cpt <- 1

    for (session in sessions) {
      files <- getDataFiles(file.path(visibility, session), recursive=TRUE)
      gpssession <- cbind(session=cpt, visibility=vis, deployment=deployment, getData(files$gps , gpscolumns))
      argossession <- cbind(session=cpt, visibility=vis, deployment=deployment, getData(files$argos, argoscolumns, is.argos=TRUE))
      tmpgps <- c(tmpgps, list(gpssession))
      tmpargos <- c(tmpargos, list(argossession))
      cpt <- cpt + 1
			deployment <- deployment + 1
    }
		
		#' GPS
    gpsdata <- rbindlist(tmpgps)
		#'# GPS : Extract latitude and longitude from gps data
		gpsdata <- extractGPSCoordinates(gpsdata)
		gpsdata[, time := as.POSIXct(time, format="%d/%m/%Y %H:%M:%S", tz="EST5EDT")]
		
    argosdata <- rbindlist(tmpargos)
		#'# Argos : project coordinates in NAD83, UTM 17
		argosdata <- projectArgosCoordinates(argosdata)
		argosdata[, time := as.POSIXct(date, tz="GMT")]
		argosdata[, time := as.POSIXct(format(time, tz="EST5EDT", usetz=TRUE))]

		
    data@gps <- gpsdata
    data@argos <- argosdata
    
    if (write) {
      write.table(gpsdata, paste(destdir,"/", "GPS_", vis,".txt",sep=""), row.names=FALSE, sep="\t")
      write.table(argosdata, paste(destdir,"/", "Argos_V", vis, ".txt", sep=""), row.names=FALSE, sep="\t")
    }
   result[[vis]] <- data
  }
  return (result)
}

getStaticVisibilities <- function(path=".") {
    print(path)
  visibilities <- list.files(path=path, include.dirs=TRUE, full.names=TRUE)
  visibilities <- visibilities[file.info(visibilities)$isdir]
  return (visibilities)
}

getStaticSessions <- function(visibility) {
  date <- list.files(path=visibility, include.dirs=TRUE)
}







